<template>
  <body>
    <div class="img">
      <img :src="logo2" alt="" />
    </div>
    <div class="texiao">
      <PaoMadeng />
    </div>
    <div class="login">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="账号" prop="phone">
          <el-input v-model="ruleForm.phone" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            v-model="ruleForm.pass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item class="bottom">
          <el-button type="primary" plain @click="submitForm(ruleFormRef)"
            >登录</el-button
          >

          <el-button @click="$router.push('/register')">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <el-alert title="success alert" type="success" center show-icon /> -->
  </body>
</template>

<script setup>
import logo2 from "../assets/logo2.png";
import { ref } from "vue";
// import { ElMessage } from "element-plus";
// import { useCounterStore } from "../stores/counter";
import axios from "axios";
import { useRouter } from "vue-router";
import { user_login } from "../utils/api";
var $router = useRouter();
const ruleFormRef = ref();

const ruleForm = ref({
  phone: "",
  pass: "",
});
const validatePhone = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入账号"));
  } else if (/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value) == false) {
    callback(new Error("请输入合法的手机号"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const rules = ref({
  pass: [{ validator: validatePass, trigger: "change" }],
  phone: [{ validator: validatePhone, trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      user_login({
        phone: ruleForm.value.phone,
        pass: ruleForm.value.pass,
      }).then((res) => {
        console.log(res.data);
        if (res.data.code == 200) {
          ElMessage({
            message: "登录成功",
            type: "success",
            center: true,
            grouping: true,
            duration: 1000,
          });
          localStorage.setItem("token", JSON.stringify(res.data.token));
          localStorage.setItem(
            "userinfo",
            JSON.stringify({
              phone: ruleForm.value.phone,
              pass: ruleForm.value.pass,
            })
          );
          // console.log(res.data.token);
          setTimeout(() => {
            $router.push("/");
          }, 1000);
        } else if (res.data.code == 500) {
          ElMessage({
            message: "登录失败",
            type: "error",
            center: true,
            grouping: true,
            duration: 1000,
          });
        }
      });

      //   console.log(ruleForm.value.phone);
    } else {
      console.log("error submit!");
      return false;
    }
  });
};
</script>


<style lang="scss" scoped>
html {
  width: 100vw;
  height: 100vh;
}
body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url(../assets/bgc.png);
  // background: red;
}
.img {
  width: 400px;
  margin: 240px auto;
  // margin-left: 900px;
  img {
    width: 100%;
  }
}
.login {
  width: 400px;
  height: 300px;
  display: flex;
  align-items: center;
  position: absolute;
  text-align: center;
  left: 50%;
  top: 55%;
  margin-top: -150px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid #ccc;
  border-radius: 10px;
  margin-left: -200px;
}
.el-button {
  margin-top: 20px;
  margin-left: 20px;
}
.texiao {
  width: 1000px;
  position: absolute;
  top: 18%;
  left: 38%;
}
</style>